﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using LunchCommand.Models;

namespace LunchCommand.Data
{
    public class LunchRepository
    {
        private LunchCommandContext Context { get; set; }

        public LunchRepository(LunchCommandContext context)
        {
            this.Context = context;
        }

        public Lunch FindLunchOfDay(DateTime date)
        {
            var tomorrow = new DateTime(date.Year, date.Month, date.Day).AddDays(1);
            var today = new DateTime(date.Year, date.Month, date.Day);

            return this.Context.Lunches.Where(l => l.LunchTime < tomorrow && l.LunchTime >= today).FirstOrDefault();
        }

        public void Save(Lunch lunch)
        {
            if (lunch.LunchId > 0)
            {
                this.Context.Entry(lunch).State = EntityState.Modified;
            }
            else
            {
                this.Context.Lunches.Add(lunch);
            }

            this.Context.SaveChanges();
        }
    }
}